﻿/*----------------------------------------------------------------
       // Copyright (C) 2008 安华顺通(北京)科技有限公司
       // 版权所有。 
       //
       // 文件名：MDIParent.cs
       // 文件功能描述：提供多文档窗口的父窗体。
       //
       // 
       // 创建标识：杨波
       //
       // 修改标识：杨波20080910
       // 修改描述：增加Logger
       //
       // 修改标识：
       // 修改描述：
//----------------------------------------------------------------*/

#region UsingStatements

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
using RuleCode.WinFormsUI.Docking;

#endregion

namespace RuleCode.ObjectTree
{
    public partial class MDIParent : Form
    {
        /*//子窗体的数目
         * private int childFormNumber = 0;
        */
        private readonly InfoWindow m_propertyWindow = new InfoWindow();
        private ObjectTreeForm m_objectTree;
        //private readonly ILog logger = LogManager.GetLogger("RuleCode.ObjectTree");

        public MDIParent()
        {
            if (!validateEnviroment())
            {
                MessageBox.Show("Configuration Error!");
                Application.Exit();

            }
            InitializeComponent();
            //log4net.Config.XmlConfigurator.Configure();
            KeyPreview = true;

            //打开新窗口的显示模式
            Util.NewFormDisplayMode = NewFormDisplayMode.MDIParent ;
             

            //Util.Logger = logger;
            //Util.DockPanel = dockPanel1;
            //Util.DockContainer = dockContainer1;
            //Util.DevArea = "GL";
            Util.DevArea = "DAT";
            Util.PropertyWindow = m_propertyWindow;
            Util.MdiParent = this;
            showAOT();
            Util.setPropertyWindow(ObjectInProp);
            //this.OnClosing += new System.EventHandler(OnMDIParentClosing);
        }

        /*private void OnMDIParentClosing(object sender, EventArgs e) { 
            //关闭程序时要做的事情

            //将最后一个Property序列化
            if (Util.CurrentProperty != null)
                Util.CurrentProperty.SerializeToXML();
        }*/
        private bool validateEnviroment()
        {
            bool ret = false;
            //初始化环境检查
            ret = true;
            return ret;

        }
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            //关闭程序时要做的事情

            //将最后一个Property序列化
            if (Util.CurrentProperty != null)
                Util.CurrentProperty.SerializeToXML();
            //Sort Table
            SysTreeView view = TreeViewOpr.getSysTreeView();
            view.ExpandAll();
            SysTreeNode root = (SysTreeNode)view.Nodes[0];
            XmlNode tables = root.SysXmlNode.SelectSingleNode("/对象树/数据字典/表");
            NodesTransaction.Instance.SortXmlNode(tables);

            //Sort EDT
            DictClass dict = new DictClass();
            NodesTransaction.Instance.SortXmlNode(dict.EDTs.SysXmlNode);

            TreeViewOpr.saveOTXML();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            /*// 创建此子窗体的一个新实例。
            Form childForm = new Form();
            // 在显示该窗体前使其成为此 MDI 窗体的子窗体。
            childForm.MdiParent = this;
            childForm.Text = "窗口" + childFormNumber++;
            childForm.Show();*/
            showAOT();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
                // TODO: 在此处添加打开文件的代码。
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
                // TODO: 在此处添加代码，将窗体的当前内容保存到一个文件中。
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: 使用 System.Windows.Forms.Clipboard 将所选的文本或图像插入到剪贴板
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: 使用 System.Windows.Forms.Clipboard 将所选的文本或图像插入到剪贴板
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: 使用 System.Windows.Forms.Clipboard.GetText() 或 System.Windows.Forms.GetData 从剪贴板中检索信息。
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        public void refreshAllOT()
        {
            foreach (Form chindForm in MdiChildren)
            {
                if (chindForm is ObjectTreeForm)
                {
                    ObjectTreeForm OTForm = (ObjectTreeForm)chindForm;
                    OTForm.refreshOT();
                }
            }
        }


        //显示在属性窗体中的对象
        private Object objectInProp;

        public Object ObjectInProp
        {
            get { return objectInProp; }
            set { objectInProp = value; }
        }

        private void ToolTip_Popup(object sender, PopupEventArgs e)
        {
        }

        private void MDIParent_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("是否退出系统?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
            {
                e.Cancel = true;
            }
            else
            {
                e.Cancel = false;
            }
        }

        private void PropertyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /**m_propertyWindow.ObjectInProp = ObjectInProp;
            m_propertyWindow.Show(dockPanel);
             **/
            Util.setPropertyWindow(ObjectInProp);
        }

        private void AOTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showAOT();
        }

        private void showAOT()
        {
            dockPanel1.DocumentStyle = DocumentStyle.SystemMdi;
            Util.DockPanel = dockPanel1;
            if (dockPanel1.DocumentStyle == DocumentStyle.SystemMdi)
            {
                m_objectTree = new ObjectTreeForm(null);
                m_objectTree.MdiParent = this;
                //Util.MdiParent = this;
                m_objectTree.ObjectInProp = ObjectInProp;
                m_objectTree.Show();
            }
            else
            {
                if (m_objectTree == null)
                {
                    m_objectTree = new ObjectTreeForm(null);
                }
                m_objectTree.ObjectInProp = ObjectInProp;
                m_objectTree.Show(dockPanel1);
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            showAOT();
            //new ToolClassForRepaire().TableSourceFile();
        }


        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Util.setPropertyWindow(ObjectInProp);
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {
            //
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            //SysWizard wizard = new SysWizard();
            //wizard.MdiParent = Util.MdiParent;
            //wizard.Show();
            //new ToolClassForRepaire().TableFieldEnumType();
        }
    }
}